<template>
  <div id="add_user_app">
    <el-dialog
      :title="form_data.id ? '编辑用户' : '添加用户'"
      :visible.sync="isShow.flag"
      width="50%"
    >
      <el-form ref="form" :model="form_data" label-width="80px" :rules="rouls">
        <el-form-item label="账号角色" prop="rolename">
          <el-select v-model="form_data.roleid" placeholder="请选择活动区域">
            <el-option
              :label="role.rolename"
              :value="role.id"
              v-for="role in roleList"
              :key="role.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号名称" prop="username">
          <el-input v-model="form_data.username"></el-input>
        </el-form-item>
        <el-form-item label="账号密码">
          <el-input v-model="form_data.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="角色状态" prop="status">
          <el-radio-group v-model="form_data.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow.flag = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  props: ["isShow"],
  data() {
    return {
      form_data: {
        roleid: "",
        username: "",
        password: "",
        status: "",
      },
      rouls: {
        rolename: {
          required: true,
          message: "角色名称不可以为空",
          trigger: "blur",
        },
        username: {
          required: true,
          message: "用户名称不可以为空",
          trigger: "blur",
        },
        status: {
          required: true,
          message: "用户状态不可以为空",
          trigger: "blur",
        },
      },
    };
  },
  computed: {
    ...mapState("role_model", ["roleList"]),
  },
  methods: {
    setData(obj){
      this.form_data = {...obj}
      this.form_data.password = ""
    },
    submit() {
      this.$axios.post("/useradd",this.form_data).then((res) => {
        if (res.code == 200) {
          this.form_data = {
            roleid: "",
            username: "",
            password: "",
            status: ""
          };
          this.isShow.flag = false;
          this.$message({
            message:'添加成功',
            type:'success'
          })
          this.$emit('reload')
        }
      })
    },
    ...mapActions("role_model", ["roleListActions"]),
  },
  created() {
    this.roleListActions();
  },
};
</script>

<style>
</style>